package history.double_exam;

import java.util.Arrays;
import java.util.Comparator;

// 5742. 将句子排序
public class LeetCode_5472 {
    public static void main(String[] args) {
        System.out.println(sortSentence("Myself2 Me1 I4 and3"));
    }

    public static String sortSentence(String s) {
        if (s == null || s.length()<=1) return s;
        String []charArray = s.split(" ");
        Arrays.sort(charArray, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.charAt(o1.length()-1)-o2.charAt(o2.length()-1);
            }
        });
        StringBuffer result = new StringBuffer();
        for (String str:charArray) {
            result.append(str.substring(0,str.length()-1));
            result.append(" ");
        }
        return result.toString().trim();
    }
}
